{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def loadDataSet(fileName):\n",
    "    numFeat = len(open(fileName).readline().split('\\t')) -1\n",
    "    dataMat = []\n",
    "    labelMat = []\n",
    "    fr = open(fileName)\n",
    "    for line in fr.readlines():\n",
    "        lineArr = []\n",
    "        curLine = line.strip().split('\\t')\n",
    "        for i in range(numFeat):\n",
    "            lineArr.append(float(curLine[i]))\n",
    "        dataMat.append(lineArr)\n",
    "        labelMat.append(float(curLine[-1]))\n",
    "    return np.array(dataMat) ,np.array(labelMat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def standRegres(xArr, yArr):\n",
    "    xMat = np.mat(xArr)\n",
    "    yMat = np.mat(yArr).T\n",
    "    xTx = xMat.T * xMat\n",
    "    if np.linalg.det(xTx) == 0.0:\n",
    "        print('this matrix is singular , cannot da inverse')\n",
    "        return \n",
    "    ws = xTx.I* (xMat.T * yMat)\n",
    "    return ws\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.      , 0.067732],\n",
       "       [1.      , 0.42781 ]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xArr , yArr = loadDataSet('ex0.txt')\n",
    "xArr[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ws = standRegres(xArr, yArr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3.00774324],\n",
       "        [1.69532264]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xMat = np.mat(xArr)\n",
    "yMat = np.mat(yArr)\n",
    "yHat = xMat * ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((200,), (200,))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(xArr)[:,1].shape ,np.array(yArr).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((200, 1), (200, 1))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xMat[:,1].shape , yMat.T[:,0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD8CAYAAABw1c+bAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4k1X2wPHvaQhQcKAodStWXEAEEdAizoCj4IJsWsUN\n13FDXNBxhir4c4FxAURHUFBEZNwRZKkoqwgOigJSyyIKigtIwAHFskiBLuf3R5Kapknzpk3XnM/z\n8NC+uXnf+z7wnNyce957RVUxxhgTPxKqugPGGGMqlwV+Y4yJMxb4jTEmzljgN8aYOGOB3xhj4owF\nfmOMiTMW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vy7DuwpqTeZ0A5+BwHS8+0TUZE7u92pg\nhqpuBlDVeLhnBf4kIgIcgjfw51duN2NLVZfgvY9wKiV+1bTA72Rjl9q2+Uu093Mz3hFDTRbxnkUk\nBbgE7zafNZ2Tf+OWQBMR+UhEskTk+krrXcVwcs9jgZOBrcBa4B5VLayc7lWZSolfTlbnNDWEiHTF\nG/i7VHVfKsFo4H5VLfQOCGu9OsDpwLlAIvCZiCxT1W+qtlsVqjuwCugGnAB8ICIfq+ruqu1WzVfT\nAr+TjV0cb/5SQzi6HxE5FZgI9FDVXyupbxXFyT2nAW/7gn5ToKeI5KtqZuV0Maac3O8W4FdV/R34\nXUSWAO2Amhr4ndzzjcAI9Sa/N4rID0ArYEXldLFKVEr8qmmpHicbu4TbFKaminjPIpIKzACuqyUj\nwIj3rKrHqWpzVW0OTAPuqKFBH5z9v34X6CIidUSkAdAJ+LqS+xlLTu55M95vOIjIEcBJwPeV2svK\nVynxq0aN+DXMxi5ONoWpqRze88PAYcDzvhFwvtbgBa4c3nOt4eR+VfVrEZkHrAEKgYmqGrIksCZw\n+G/8KPCKiKzFW+Vyv6rW6BU7RWQycA7QVES2AI8Abqjc+GVP7hpjTJypaakeY4wx5WSB3xhj4owF\nfmOMiTMW+I0xJs5Y4DfGmDhjgd8YY+KMBX5jjIkzFviNMSbO/D9fE+XW80jgMAAAAABJRU5ErkJg\ngg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x24d11a60898>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.scatter(np.array(xArr)[:,1] ,np.array(yArr))\n",
    "xCopy = xMat.copy()\n",
    "xCopy.sort(0)\n",
    "yHat = xCopy * ws\n",
    "ax.plot(xCopy[:, 1],yHat ,'r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.13653777],\n",
       "       [0.13653777, 1.        ]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(yHat.T , yMat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((200, 1), (1, 200))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yHat.shape , yMat.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def lwlr(testPoint , xArr ,yArr ,k=1.0):\n",
    "    xMat = np.mat(xArr )\n",
    "    yMat = np.mat(yArr).T\n",
    "    m = xMat.shape[0]\n",
    "    weights = np.mat(np.eye((m)))\n",
    "    for j in range(m):\n",
    "        diffMat = testPoint - xMat[j,:]\n",
    "        weights[ j, j] = np.exp(diffMat * diffMat.T /(-2.0 * k ** 2))\n",
    "    xTx = xMat.T * (weights * xMat)\n",
    "    if np.linalg.det(xTx) == 0.0:\n",
    "        print('this matrix is singular , cannot da inverse')\n",
    "        return \n",
    "    ws = xTx.I* (xMat.T * (weights * yMat))\n",
    "    return testPoint * ws\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def lwlrTest(testArr , xArr ,yArr,k=1.0):\n",
    "    m = np.array(testArr).shape[0]\n",
    "    yHat = np.zeros(m)\n",
    "    for i in range(m):\n",
    "        yHat[i] = lwlr(testArr[i] , xArr ,yArr, k)\n",
    "    return yHat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.176513"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xArr , yArr = loadDataSet('ex0.txt')\n",
    "yArr[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单个点的描 述 lwlr， "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3.20366661]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lwlr(xArr[0] , xArr, yArr , 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((200, 2), (200, 2), (200,))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xArr.shape , xArr.shape , yArr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "yHat = lwlrTest(xArr , xArr , yArr ,0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xMat = np.mat(xArr)\n",
    "srtInd = xMat[:,1].argsort(0)\n",
    "xSort = xMat[srtInd][:,0,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8VOX1+PHPyUYWAoFsQEIIEEBQFiWKUVstrQ1asS21\n1WprW+2PUtfSWreqadS22u/3W9xqLVq3ql9bLXWrQv0quJAEGmTfl4SQBEIgGwkh25zfHzORELJM\nSCaz5Lxfr3lNZu4z955LeJ15cu7zPFdUFWOMMYElyNsBGGOM6XuW3I0xJgB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TJ3RhjApAld2OMCUCW3I0xJgBZcjfGmAD0/wGFu+TtvGRc5gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x24d11a73a90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.plot(xSort[:,1] , yHat[srtInd] ,'y')\n",
    "ax.scatter(np.array(xArr)[:,1] ,np.array(yArr),c='r',s=2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ressError(yArr ,yHatArr):\n",
    "    return ((yArr - yHatArr )** 2).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "abX , abY = loadDataSet('abalone.txt')\n",
    "yHat01 = lwlrTest(abX[:99] ,abX[:99],abY[:99],0.1)\n",
    "yHat1  = lwlrTest(abX[:99] ,abX[:99],abY[:99],1)\n",
    "yHat10  = lwlrTest(abX[:99] ,abX[:99],abY[:99],10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(56.811168577945885, 429.8905618702014, 549.118170882589)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ressError(abY[:99] , yHat01.T),\\\n",
    "ressError(abY[:99] , yHat1.T),\\\n",
    "ressError(abY[:99] , yHat10.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60571.25199167674"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yHat01  = lwlrTest(abX[100:199] ,abX[0:99],abY[0:99],0.1)\n",
    "ressError(abY[100:199] , yHat01.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "517.5711905380425"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yHat10  = lwlrTest(abX[100:199] ,abX[0:99],abY[0:99],10)\n",
    "ressError(abY[100:199] , yHat10.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "573.5261441896967"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yHat1  = lwlrTest(abX[100:199] ,abX[0:99],abY[0:99],1)\n",
    "ressError(abY[100:199] , yHat1.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "518.6363153239507"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws = standRegres(abX[0:99],abY[0:99])\n",
    "yHat = np.mat(abX[100:199])*ws\n",
    "ressError(abY[100:199] , np.array(yHat).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEBpJREFUeJzt3X9sXWd9x/H3d66ZzH45pV6UhB8BjXkLSEvA6pg2EBMb\nLt2mhCGh9Q/UbWgBiVWwH5aaIY3+NQFehzQJgYpa0U1QQCINFWIY6CbYpAFzcGhSitdSWlEnNEaV\nBRJXLPW+++MeRzdeHPv6nOvr+9z3S7ryuc859znfc3Ly8b3POec6MhNJUjl+qt8FSJKaZbB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z7WGvjx2kv6uzFMboTD+88laTCDOvJU0kq\nlsEuSYUx2CWpMAa7JBXGYJekwhjsklQYg12SCmOwS1Jh/g9G26rjP82B3gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x24d11b056d8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.scatter([1]*8 , ws)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ridgeRegres(xMat,yMat , lam=0.2):\n",
    "    xTx = xMat.T * xMat\n",
    "    denom = xTx + np.eye(xMat.shape[1])*lam\n",
    "    if np.linalg.det(denom) == 0.0:\n",
    "        print('this matrix is singular , cannot da inverse')\n",
    "        return\n",
    "    ws = denom.I * (xMat.T * yMat)\n",
    "    return ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ridgeTest(xArr ,yArr ):\n",
    "    xMat = np.mat(xArr)\n",
    "    yMat = np.mat(yArr).T\n",
    "    yMean = np.mean(yMat,0)\n",
    "    yMat = yMat- yMean\n",
    "    xMeans = np.mean(xMat ,0)\n",
    "    xVar = np.var(xMat , 0)\n",
    "    xMat = (xMat - xMeans) / xVar\n",
    "    numTestPts =30\n",
    "    wMat = np.zeros((numTestPts ,xMat.shape[1]))\n",
    "    for i in range(numTestPts):\n",
    "        ws = ridgeRegres(xMat , yMat,np.exp(i -10))\n",
    "        wMat[i,:] = ws.T\n",
    "    return wMat\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((4177, 8), (4177,))"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ridgeWeights = ridgeTest(abX, abY)\n",
    "abX.shape, abY.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((30, 8), (4177, 8))"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ridgeWeights.shape , abX.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD8CAYAAAB+UHOxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8XGd56PHfc86s2rfR4kW2JNtJvMuVlcRAEkgAJ01I\nKBCWQsuF25ASWtrL7S2Fbpd7uZfey20LlCYECDQtlNKyBWIIcRY7CSHxGq9JLNuyJVmyJFu21lnP\ne/+Y0ViyJW8z0mhmnu8n53POec+ZeZ+TI59n3vdsYoxBKaVU/rEyHYBSSqnM0ASglFJ5ShOAUkrl\nKU0ASimVpzQBKKVUntIEoJRSeUoTgFJK5SlNAEoplac0ASilVJ5yZTqAi6mqqjKLFy/OdBhKKZU1\nduzY0W+MCVzOunM6ASxevJjt27dnOgyllMoaInLsctfVLiCllMpTmgCUUipPaQJQSqk8pQlAKaXy\nlCYApZTKU5oAlFIqT2kCUEqpPDWn7wO4WtseP4oTy/CrLmWGv1ImVyBT1Bcvk+SHJ64jiRkRQazE\nvCTmBcRKjBPlli3xwbKwXYJlW8ky2zV52uN34fG5cHmsZD1KqbknJxPAzl8eJxqOZS4Afc0yEE84\nHr8Lt8/Gm0gKbp8Lj9/G43NRXOGjrKaAsho/pYEC3F470yErlVdyMgF87Es3ZzqEGWXMeRnGTDFp\nzITpcwsM5ty0SXxXYjw+b5zJ5Y5jMI7BicWHWNRJTjsxh9iE6WjYIRKKER6LEg5GCQdjRMbi49BY\nlO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     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.plot(ridgeWeights)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def regularize(xMat):#regularize by columns\n",
    "    inMat = xMat.copy()\n",
    "    inMeans = mean(inMat,0)   #calc mean then subtract it off\n",
    "    inVar = var(inMat,0)      #calc variance of Xi then divide by it\n",
    "    inMat = (inMat - inMeans)/inVar\n",
    "    return inMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def stageWise(xArr , yArr , eps=0.01,numIt = 100):\n",
    "    xMat= np.mat(xArr)\n",
    "    yMat = np.mat(yArr).T\n",
    "    yMean = np.mean(yMat , 0)\n",
    "    yMat = yMat - yMean\n",
    "    xMat = regularize(xMat)\n",
    "    m , n = xMat.shape\n",
    "    returnMat = np.zeros((numIt ,n))\n",
    "    ws = np.zeros((n,1))\n",
    "    wsTest = ws.copy()\n",
    "    wsMax = ws.copy()\n",
    "    for i in range(numIt):\n",
    "        print (ws.T)\n",
    "        lowestError = np.inf; \n",
    "        for j in range(n):\n",
    "            for sign in [-1,1]:\n",
    "                wsTest = ws.copy()\n",
    "                wsTest[j] += eps*sign\n",
    "                yTest = xMat*wsTest\n",
    "                rssE = ressError(yMat.A,yTest.A)\n",
    "                if rssE < lowestError:\n",
    "                    lowestError = rssE\n",
    "                    wsMax = wsTest\n",
    "        ws = wsMax.copy()\n",
    "        returnMat[i:] = ws.T\n",
    "    return returnMat\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "xArr , yArr = loadDataSet('abalone.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0. 0. 0. 0. 0.]]\n",
      "[[0.   0.   0.   0.01 0.   0.   0.   0.  ]]\n",
      "[[0.   0.   0.   0.02 0.   0.   0.   0.  ]]\n",
      "[[0.   0.   0.   0.03 0.   0.   0.   0.  ]]\n",
      "[[0.   0.   0.   0.04 0.   0.   0.   0.  ]]\n",
      "[[0.   0.   0.   0.05 0.   0.   0.   0.  ]]\n",
      "[[0.   0.   0.   0.06 0.   0.   0.   0.  ]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.  ]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.01]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.02]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.03]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.04]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.05]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.06]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.07]]\n",
      "[[0.   0.   0.01 0.06 0.   0.   0.   0.08]]\n",
      "[[0.   0.   0.01 0.05 0.   0.   0.   0.08]]\n",
      "[[0.   0.   0.01 0.05 0.   0.   0.   0.09]]\n",
      "[[0.   0.   0.01 0.05 0.   0.   0.   0.1 ]]\n",
      "[[0.   0.   0.01 0.05 0.   0.   0.   0.11]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.01  0.    0.11]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.02  0.    0.11]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.02  0.    0.12]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.03  0.    0.12]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.03  0.    0.13]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.04  0.    0.13]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.05  0.    0.13]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.05  0.    0.14]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.06  0.    0.14]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.07  0.    0.14]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.07  0.    0.15]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.08  0.    0.15]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.08  0.    0.16]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.09  0.    0.16]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.1   0.    0.16]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.1   0.    0.17]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.11  0.    0.17]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.12  0.    0.17]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.12  0.    0.18]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.13  0.    0.18]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.13  0.    0.19]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.14  0.    0.19]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.15  0.    0.19]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.15  0.    0.2 ]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.16  0.    0.2 ]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.16  0.    0.21]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.17  0.    0.21]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.18  0.    0.21]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.18  0.    0.22]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.19  0.    0.22]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.2   0.    0.22]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.2   0.    0.23]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.21  0.    0.23]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.21  0.    0.24]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.22  0.    0.24]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.23  0.    0.24]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.23  0.    0.25]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.24  0.    0.25]]\n",
      "[[ 0.    0.    0.01  0.05  0.   -0.25  0.    0.25]]\n",
      "[[ 0.    0.    0.02  0.05  0.   -0.25  0.    0.25]]\n",
      "[[ 0.    0.    0.02  0.04  0.   -0.25  0.    0.25]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.25  0.    0.25]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.25  0.    0.26]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.26  0.    0.26]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.26  0.    0.27]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.27  0.    0.27]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.28  0.    0.27]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.28  0.    0.28]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.29  0.    0.28]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.29  0.    0.29]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.3   0.    0.29]]\n",
      "[[ 0.    0.    0.03  0.04  0.   -0.31  0.    0.29]]\n",
      "[[ 0.    0.    0.04  0.04  0.   -0.31  0.    0.29]]\n",
      "[[ 0.    0.    0.04  0.04  0.   -0.32  0.    0.29]]\n",
      "[[ 0.    0.    0.04  0.04  0.   -0.33  0.    0.29]]\n",
      "[[ 0.    0.    0.04  0.04  0.   -0.33  0.    0.3 ]]\n",
      "[[ 0.    0.    0.04  0.04  0.   -0.34  0.    0.3 ]]\n",
      "[[ 0.    0.    0.04  0.04  0.   -0.35  0.    0.3 ]]\n",
      "[[ 0.    0.    0.05  0.04  0.   -0.35  0.    0.3 ]]\n",
      "[[ 0.    0.    0.05  0.04  0.   -0.36  0.    0.3 ]]\n",
      "[[ 0.    0.    0.05  0.04  0.   -0.37  0.    0.3 ]]\n",
      "[[ 0.    0.    0.05  0.04  0.   -0.37  0.    0.31]]\n",
      "[[ 0.    0.    0.05  0.04  0.   -0.38  0.    0.31]]\n",
      "[[ 0.    0.    0.05  0.04  0.   -0.39  0.    0.31]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.39  0.    0.31]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.4   0.    0.31]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.41  0.    0.31]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.41  0.    0.32]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.42  0.    0.32]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.42  0.    0.33]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.43  0.    0.33]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.44  0.    0.33]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.44  0.    0.34]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.45  0.    0.34]]\n",
      "[[ 0.    0.    0.06  0.04  0.   -0.46  0.    0.34]]\n",
      "[[ 0.    0.    0.07  0.04  0.   -0.46  0.    0.34]]\n",
      "[[ 0.    0.    0.07  0.03  0.   -0.46  0.    0.34]]\n",
      "[[ 0.    0.    0.08  0.03  0.   -0.46  0.    0.34]]\n",
      "[[ 0.    0.    0.08  0.03  0.   -0.46  0.    0.35]]\n",
      "[[ 0.    0.    0.08  0.03  0.   -0.47  0.    0.35]]\n",
      "[[ 0.    0.    0.08  0.03  0.   -0.47  0.    0.36]]\n",
      "[[ 0.    0.    0.08  0.03  0.   -0.48  0.    0.36]]\n",
      "[[ 0.    0.    0.08  0.03  0.   -0.49  0.    0.36]]\n",
      "[[ 0.    0.    0.09  0.03  0.   -0.49  0.    0.36]]\n",
      "[[ 0.    0.    0.09  0.03  0.   -0.5   0.    0.36]]\n",
      "[[ 0.    0.    0.09  0.03  0.   -0.51  0.    0.36]]\n",
      "[[ 0.    0.    0.09  0.03  0.   -0.51  0.    0.37]]\n",
      "[[ 0.    0.    0.09  0.03  0.   -0.52  0.    0.37]]\n",
      "[[ 0.    0.    0.09  0.03  0.01 -0.52  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.01 -0.52  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.01 -0.53  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.02 -0.53  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.03 -0.53  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.04 -0.53  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.04 -0.54  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.05 -0.54  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.06 -0.54  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.06 -0.55  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.07 -0.55  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.08 -0.55  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.08 -0.56  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.09 -0.56  0.    0.37]]\n",
      "[[ 0.01  0.    0.09  0.03  0.1  -0.56  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.1  -0.56  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.1  -0.57  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.11 -0.57  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.12 -0.57  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.13 -0.57  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.13 -0.58  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.14 -0.58  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.15 -0.58  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.15 -0.59  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.16 -0.59  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.17 -0.59  0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.17 -0.6   0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.18 -0.6   0.    0.37]]\n",
      "[[ 0.02  0.    0.09  0.03  0.19 -0.6   0.    0.37]]\n",
      "[[ 0.03  0.    0.09  0.03  0.19 -0.6   0.    0.37]]\n",
      "[[ 0.03  0.    0.09  0.03  0.19 -0.61  0.    0.37]]\n",
      "[[ 0.03  0.    0.09  0.03  0.2  -0.61  0.    0.37]]\n",
      "[[ 0.03  0.    0.09  0.03  0.21 -0.61  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.21 -0.61  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.22 -0.61  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.22 -0.62  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.23 -0.62  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.24 -0.62  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.24 -0.63  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.25 -0.63  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.26 -0.63  0.    0.37]]\n",
      "[[ 0.04  0.    0.09  0.03  0.26 -0.63  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.27 -0.63  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.28 -0.63  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.29 -0.63  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.29 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.3  -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.05  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n",
      "[[ 0.04  0.    0.09  0.03  0.31 -0.64  0.    0.36]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0.  ,  0.  ,  0.  , ...,  0.  ,  0.  ,  0.  ],\n",
       "       [ 0.  ,  0.  ,  0.  , ...,  0.  ,  0.  ,  0.  ],\n",
       "       [ 0.  ,  0.  ,  0.  , ...,  0.  ,  0.  ,  0.  ],\n",
       "       ...,\n",
       "       [ 0.05,  0.  ,  0.09, ..., -0.64,  0.  ,  0.36],\n",
       "       [ 0.04,  0.  ,  0.09, ..., -0.64,  0.  ,  0.36],\n",
       "       [ 0.05,  0.  ,  0.09, ..., -0.64,  0.  ,  0.36]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stageWise(xArr, yArr, 0.01,200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  0.08861935,   6.16314281,  13.34649509,  14.54157603,\n",
       "           8.84433835, -21.24991205, -11.59911917,   6.61777829]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xMat = np.mat(xArr)\n",
    "yMat = np.mat(yArr)\n",
    "# xMat = regularize(xMat)\n",
    "# yMean = np.mean(yMat,0)\n",
    "# yMat = yMat - yMean\n",
    "weights =  standRegres(xMat , yMat)\n",
    "weights.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((4177, 8), (1, 4177))"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xMat.shape,yMat.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((58, 4), (1, 58))"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lgX = xMat[:58,:4]\n",
    "lgY = yMat.T[:58,0].T\n",
    "lgX.shape,lgY.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lgX1 = np.mat(np.ones((58,5))) \n",
    "lgX1[:,1:5] = lgX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1.   , 1.   , 0.455, 0.365, 0.095]])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lgX1[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-0.41840538],\n",
       "        [-0.10870462],\n",
       "        [-0.66376502],\n",
       "        [32.78914166],\n",
       "        [-9.55485701]])"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws = standRegres(lgX1 , lgY)\n",
    "ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[10.2312022]]), 15.0)"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lgX1[0] *ws , lgY[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[10.49483428]]), 8.0)"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lgX1[-1]*ws ,lgY[0,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[3.83837691]]), 5.0)"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lgX1[43]*ws ,lgY[0,43]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((58, 5), (1, 58))"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lgX1.shape, lgY.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ws = ridgeTest(np.array(lgX),np.array(lgY))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def crossValidation(xArr,yArr,numVal=10):\n",
    "    m = len(yArr) \n",
    "    indexList = np.arange(m)\n",
    "    errorMat = np.zeros((numVal,30))\n",
    "    for i in range(numVal):\n",
    "        trainX=[]; trainY=[]\n",
    "        testX = []; testY = []\n",
    "        np.random.shuffle(indexList)\n",
    "        for j in range(m):\n",
    "            if j < m*0.9: \n",
    "                trainX.append(xArr[indexList[j]])\n",
    "                trainY.append(yArr[indexList[j]])\n",
    "            else:\n",
    "                testX.append(xArr[indexList[j]])\n",
    "                testY.append(yArr[indexList[j]])\n",
    "        wMat = ridgeTest(trainX , np.array(trainY).T)\n",
    "        for k in range(30):\n",
    "            matTestX = np.mat(testX); matTrainX=np.mat(trainX)\n",
    "            meanTrain = np.mean(matTrainX,0)\n",
    "            varTrain = var(matTrainX,0)\n",
    "            matTestX = (matTestX-meanTrain)/varTrain \n",
    "            yEst = matTestX * mat(wMat[k,:]).T + np.mean(trainY)\n",
    "            errorMat[i,k]=ressError(yEst.T.A,np.array(testY)) \n",
    "    meanErrors = np.mean(errorMat,0)\n",
    "    minMean = float(min(meanErrors))\n",
    "    bestWeights = wMat[np.nonzero(meanErrors==minMean)]\n",
    "    xMat = np.mat(xArr); yMat=np.mat(yArr).T\n",
    "    meanX = np.mean(xMat,0); varX = np.var(xMat,0)\n",
    "    unReg = bestWeights / varX\n",
    "    print (\"the best model from Ridge Regression is:\\n\",unReg)\n",
    "    print (\"with constant term: \",-1*sum(multiply(meanX,unReg)) + mean(yMat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "crossValidation(np.array(lgX),np.array(lgY).T,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
